package com.xscha.tableforge.utils;


import com.xscha.tableforge.constants.IdRulePrefixEnum;
import com.xscha.tableforge.constants.NodeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * id 生成器
 *
 * @author Vondser
 */
@Component
public class IdUtil {

    public static final Integer DATASHEET_ID_FIXED_LENGTH = 15;

    public static final Integer ID_FIXED_LENGTH = 12;

    public static final int SHARE_ID_LENGTH = 18;

    public static final Integer AUTOMATION_ID_FIXED_LENGTH = 15;

	private static SnowflakeIdGenerator snowflakeIdGenerator;

	@Autowired
	public void setSnowflakeIdGenerator(SnowflakeIdGenerator snowflakeIdGenerator) {
		IdUtil.snowflakeIdGenerator = snowflakeIdGenerator;
	}

	/** 生成Space空间ID */
    public static String createSpaceId() {
        return IdRulePrefixEnum.SPC.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
    }

	/** 生成应用ID */
	public static String createAppId() {
		return IdRulePrefixEnum.APP.getIdRulePrefixEnum()
				+ RandomExtendUtil.randomString(ID_FIXED_LENGTH);
	}

	/** 生成工作表ID */
	public static String createDstId() {
		return IdRulePrefixEnum.DST.getIdRulePrefixEnum()
				+ RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
	}
	/** 生成字段ID */
	public static String createFieldId() {
		return IdRulePrefixEnum.FLD.getIdRulePrefixEnum()
				+ createSnowflakeId();
	}
	/** 生成表记录ID */
	public static String createRecordId() {
		return IdRulePrefixEnum.REC.getIdRulePrefixEnum()
				+ createSnowflakeId();
	}

	/**
	 * 使用雪花算法创建ID
	 */
	public static String createSnowflakeId() {
		if (snowflakeIdGenerator == null) {
			// 如果没有注入，则创建一个默认的生成器
			snowflakeIdGenerator = new SnowflakeIdGenerator();
		}
		return snowflakeIdGenerator.nextIdStr();
	}

	/**
	 * 使用雪花算法创建ID
	 */
	public static Long createSnowflakeLongId() {
		if (snowflakeIdGenerator == null) {
			// 如果没有注入，则创建一个默认的生成器
			snowflakeIdGenerator = new SnowflakeIdGenerator();
		}
		return snowflakeIdGenerator.nextLongId();
	}

	/**
	 * 使用雪花算法创建关联元数据ID
	 */
	public static String createRelationMetaId() {
		return IdRulePrefixEnum.REF.getIdRulePrefixEnum() + createSnowflakeId();
	}

	/** 生成表OptionMapping ID */
	public static String createOptionId() {
		return IdRulePrefixEnum.OPT.getIdRulePrefixEnum()
				+ RandomExtendUtil.randomString(ID_FIXED_LENGTH);
	}

	/** 生成操作日志ID */
	public static String createLogId() {
		return IdRulePrefixEnum.LOG.getIdRulePrefixEnum()
				+ createSnowflakeId();
	}



	public static Long createNodeId() {
        return createSnowflakeLongId();
    }

    /**
     * create node id.
     *
     * @param nodeType node type
     * @return String
     */
    public static String createNodeId(NodeType nodeType) {
        return switch (nodeType) {
            case DATASHEET -> IdRulePrefixEnum.DST.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            case FORM -> IdRulePrefixEnum.FORM.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            case DASHBOARD -> IdRulePrefixEnum.DASHBOARD.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            case MIRROR -> IdRulePrefixEnum.MIRROR.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            case AI_CHAT_BOT -> IdRulePrefixEnum.AI.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            case AUTOMATION -> IdRulePrefixEnum.AUTOMATION.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            case AIRAGENT -> IdRulePrefixEnum.AIRAGENT.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            case CUSTOM_PAGE -> IdRulePrefixEnum.CUSTOM_PAGE.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
            default -> IdRulePrefixEnum.FOD.getIdRulePrefixEnum()
                + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
        };
    }

    public static String createDocumentName() {
        return IdRulePrefixEnum.DOCUMENT_NAME.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
    }

    public static String createViewId() {
        return IdRulePrefixEnum.VIW.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
    }

    public static String createShareId() {
        return IdRulePrefixEnum.SHARE.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(SHARE_ID_LENGTH);
    }

    public static String createTempCatCode() {
        return IdRulePrefixEnum.TPC.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
    }

    public static String createTemplateId() {
        return IdRulePrefixEnum.TPL.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
    }

    public static String createWidgetId() {
        return IdRulePrefixEnum.WIDGET.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(DATASHEET_ID_FIXED_LENGTH);
    }

    public static String createWidgetPackageId() {
        return IdRulePrefixEnum.WIDGET_PACKAGE.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
    }

    public static String createAutomationServiceId() {
        return IdRulePrefixEnum.AUTOMATION_SERVICE.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(AUTOMATION_ID_FIXED_LENGTH);
    }

    public static String createAutomationTriggerTypeId() {
        return IdRulePrefixEnum.AUTOMATION_TRIGGER_TYPE.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(AUTOMATION_ID_FIXED_LENGTH);
    }

    public static String createAutomationActionTypeId() {
        return IdRulePrefixEnum.AUTOMATION_ACTION_TYPE.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(AUTOMATION_ID_FIXED_LENGTH);
    }

    public static String createAutomationRobotId() {
        return IdRulePrefixEnum.AUTOMATION_ROBOT.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(AUTOMATION_ID_FIXED_LENGTH);
    }

    public static String createAutomationTriggerId() {
        return IdRulePrefixEnum.AUTOMATION_TRIGGER.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(AUTOMATION_ID_FIXED_LENGTH);
    }

    public static String createAutomationActionId() {
        return IdRulePrefixEnum.AUTOMATION_ACTION.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(AUTOMATION_ID_FIXED_LENGTH);
    }

    /**
     * template tag code.
     */
    public static String createTempTagCode() {
        return IdRulePrefixEnum.TPT.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
    }

    /**
     * template album custom id.
     */
    public static String createTemplateAlbumId() {
        return IdRulePrefixEnum.ALB.getIdRulePrefixEnum()
            + RandomExtendUtil.randomString(ID_FIXED_LENGTH);
    }

    /**
     * is form.
     *
     * @param id node id
     * @return boolean
     */
    public static boolean isForm(String id) {
        if (null == id) {
            return false;
        }
        return id.startsWith(IdRulePrefixEnum.FORM.getIdRulePrefixEnum());
    }

    /**
     * is embed.
     *
     * @param id node id
     * @return boolean
     */
    public static boolean isEmbed(String id) {
        if (null == id) {
            return false;
        }
        return id.startsWith(IdRulePrefixEnum.EMB.getIdRulePrefixEnum());
    }
}
